/**
 * @project sky-take-out_1
 * @author MECHREVO
 * @date 2023/12/10 14:20:15
 * @version 1.0
 */
package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ShoppingCartImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //菜品
        if(Objects.nonNull(shoppingCartDTO.getDishId())){
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setDishId(shoppingCartDTO.getDishId());
            shoppingCartQuery.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCartQuery.setUserId(BaseContext.getCurrentId());
            ShoppingCart shoppingCart=shoppingCartMapper.selectOne(shoppingCartQuery);
            //c查看以前是否添加过，没有就是新增
            if(Objects.isNull(shoppingCart)){
                shoppingCart = new ShoppingCart();
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }else{
                //如果以前加购过，就是更新数量
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
        //是套餐
        if(Objects.nonNull(shoppingCartDTO.getSetmealId())){
            ShoppingCart shoppingCartQuery = new ShoppingCart();
            shoppingCartQuery.setSetmealId(shoppingCartDTO.getSetmealId());
            shoppingCartQuery.setUserId(BaseContext.getCurrentId());
            ShoppingCart shoppingCart=shoppingCartMapper.selectOne(shoppingCartQuery);
            //c查看以前是否添加过，没有就是新增
            if(Objects.isNull(shoppingCart)){
                shoppingCart = new ShoppingCart();
                SetmealVO setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setNumber(1);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartMapper.insert(shoppingCart);
            }
            else{
                //如果以前加购过，就是更新数量
                shoppingCart.setNumber(shoppingCart.getNumber()+1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
    }

    /**
     * 增加购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.selectlist(shoppingCart);

    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.clean(shoppingCart);
    }

    /**
     * 减少一个
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCartQuery = ShoppingCart.builder()
  //              .id(BaseContext.getCurrentId())
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
               .setmealId(shoppingCartDTO.getSetmealId())
               .dishFlavor(shoppingCartDTO.getDishFlavor())
               .build();
        ShoppingCart select = shoppingCartMapper.select(shoppingCartQuery);

        if(select.getNumber()==1){
            shoppingCartMapper.deleteById(shoppingCartQuery);
        }else {
            select.setNumber(select.getNumber()-1);
            shoppingCartMapper.updateById(select);

        }
    }
}
